//
//  FinishedTableView.m
//  1gene
//
//  Created by 1jiyin on 16/3/24.
//  Copyright © 2016年 1jiyin. All rights reserved.
//

#import "FinishedTableView.h"
#import "UILabel+VerticalAlign.h"
@implementation FinishedTableView
-(instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style
{
    if (self=[super initWithFrame:frame style:style]) {
        self.delegate=self;
        self.dataSource=self;
        self.showsVerticalScrollIndicator=NO;
        self.tableHeaderView=[UIView new];
        self.tableFooterView=[UIView new];
       // [self initData];
    }
    return self;
}
-(void)initData
{
    NSArray *arr=@[@{@"id":@"1001",@"imageUrl":@"myBackground.jpg",@"title":@"心脑血管",@"content":@"天赋基因天赋基因天赋基因天赋基因天赋基因天",@"price":@"1599"},@{@"id":@"1002",@"imageUrl":@"myBackground.jpg",@"title":@"天赋基因",@"content":@"比较郁闷的是，UILabel并不提供设置其垂直对齐方式的选项。所以如果",@"price":@"1000"},@{@"id":@"1003",@"imageUrl":@"myBackground.jpg",@"title":@"心脑血管",@"content":@"2015年卫生组织发现高血压2015年卫生组织发现高血压2015年卫生组织发现高血压",@"price":@"2600"},@{@"id":@"1004",@"imageUrl":@"myBackground.jpg",@"title":@"天赋基因",@"content":@"2015年卫生组织发现高血压",@"price":@"800"}];
    _dataArray=[NSMutableArray arrayWithArray:arr];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell==nil) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    for (id subView in cell.contentView.subviews) {
        [subView removeFromSuperview];
    }
    TradeModel *model=_dataArray[indexPath.row];
    
    
    UIImageView *imgView=[[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 70, 50)];
    //imgView.image=[UIImage imageNamed:_dataArray[indexPath.row][@"imageUrl"]];
    NSString *url=[NSString stringWithFormat:@"%@",model.good[@"image_url"]];
    url=[url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    [imgView sd_setImageWithURL:[NSURL URLWithString:url] placeholderImage:[UIImage imageNamed:@"noImage_background"]];
    [cell.contentView addSubview:imgView];
    //标题
    UILabel *title=[[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(imgView.frame)+3, imgView.frame.origin.y, self.frame.size.width-(CGRectGetMaxX(imgView.frame)+3)-80, 50/3.0)];
    title.text=model.good[@"name"];
    title.textColor=[UIColor blackColor];
    title.font=[UIFont systemFontOfSize:15];
    [cell.contentView addSubview:title];
    
    
    //内容
    UILabel *content=[[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(imgView.frame)+3, CGRectGetMaxY(title.frame), self.frame.size.width-(CGRectGetMaxX(imgView.frame)+3)-80, (50/3.0)*2)];
    content.text=model.good[@"short_discription"];
    content.textColor=[UIColor grayColor];
    content.font=[UIFont systemFontOfSize:13.5];
    content.numberOfLines=2;
    [content alignTop];
    [cell.contentView addSubview:content];
    
    //去结算
    UIButton *payMoneyBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    payMoneyBtn.frame=CGRectMake(CGRectGetMaxX(title.frame)+10, content.frame.origin.y, 60, 20);
    [payMoneyBtn setTitle:@"查看详情" forState:UIControlStateNormal];
    [payMoneyBtn setTitleColor:[UIColor colorWithRed:232/255.0 green:151/255.0 blue:0 alpha:1] forState:UIControlStateNormal];
    payMoneyBtn.layer.borderColor=[UIColor colorWithRed:232/255.0 green:151/255.0 blue:0 alpha:1].CGColor;
    payMoneyBtn.layer.borderWidth=1.0;
    payMoneyBtn.layer.cornerRadius=5.0;
    payMoneyBtn.titleLabel.font=[UIFont systemFontOfSize:12];
    payMoneyBtn.tag=indexPath.row;
    [payMoneyBtn addTarget:self action:@selector(tradeDetail:) forControlEvents:UIControlEventTouchUpInside];
    [cell.contentView addSubview:payMoneyBtn];
    
    //分割线
    UIView *line=[[UIView alloc]initWithFrame:CGRectMake(0, 70-1, self.frame.size.width, 1)];
    line.backgroundColor=[UIColor colorWithRed:235/255.0 green:235/255.0 blue:241/255.0 alpha:1];
    [cell.contentView addSubview:line];
    return cell;
    return cell;
}


-(void)tradeDetail:(UIButton *)sender
{
    TradeModel *model=_dataArray[sender.tag];
    [self.myDelegate gotoOkTradeDetail:model.tradeId];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    TradeModel *model=_dataArray[indexPath.row];
    [self.myDelegate gotoOkTradeDetail:model.tradeId];
}



-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 70;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
